package com.cc.moxingvoice.ui.window;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.superc.cframework.utils.LogUtil;

/**
 * 创建日期：2018/2/27 on 9:16
 * 描述：
 * 作者：郭士超
 * QQ：1169380200
 */

public class MyWindowManager {

    /**
     * 小悬浮窗View的实例
     */
    private static FloatWindowControlView controlWindow;

    /**
     * 小悬浮窗View的参数
     */
    private static WindowManager.LayoutParams controlWindowParams;

    /**
     * 音频悬浮窗View的实例
     */
    private static FloatWindowVoiceView voiceWindow;

    /**
     * 音频悬浮窗View的参数
     */
    private static WindowManager.LayoutParams voiceWindowParams;

    /**
     * 用于控制在屏幕上添加或移除悬浮窗
     */
    private static WindowManager mWindowManager;

    /**
     * 记录控制悬浮窗是否显示
     */
    public static boolean showControlWindow;

    /**
     * 记录音频悬浮窗是否显示
     */
    public static boolean showVoiceWindow;

    // 辅助小窗口
    private static View mStatusBarHelperView;

    // 屏幕高宽
    public static int screenWidth;
    public static int screenHeight;

    // 启动方式
    public static final int GAME = 1;
    public static final int QQ = 2;
    public static final int WX = 3;

    // 当前启动方式
    private static int START_TYPE = GAME;

    /**
     * 创建一个悬浮窗。初始位置为屏幕的左部中间靠上位置。
     *
     * @param context 必须为应用程序的Context.
     */
    public static void createControlWindow(Context context) {
        WindowManager windowManager = getWindowManager(context);
        screenWidth = windowManager.getDefaultDisplay().getWidth();
        screenHeight = windowManager.getDefaultDisplay().getHeight();
        if (controlWindow == null) {
            controlWindow = new FloatWindowControlView(context);
            if (controlWindowParams == null) {
                controlWindowParams = new WindowManager.LayoutParams();
                if (Build.VERSION.SDK_INT >= 26) { //8.0新特性
                    controlWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                } else {
//                    controlWindowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
                    controlWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
                }
                controlWindowParams.format = PixelFormat.RGBA_8888;
                controlWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                controlWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
                controlWindowParams.width = FloatWindowControlView.viewWidth;
                controlWindowParams.height = FloatWindowControlView.viewHeight;
                controlWindowParams.x = 0; // 0为左 screenWidth为右
                controlWindowParams.y = screenHeight / 3;
            }
            controlWindow.setParams(controlWindowParams);
            windowManager.addView(controlWindow, controlWindowParams);
        }
        showControlWindow = true;
    }

    /**
     * 将悬浮窗从屏幕上移除。
     *
     * @param context 必须为应用程序的Context.
     */
    public static void removeControlWindow(Context context) {
        if (controlWindow != null) {
            WindowManager windowManager = getWindowManager(context);
            windowManager.removeView(controlWindow);
            controlWindow = null;
        }
        showControlWindow = false;
    }

    /**
     * 创建一个悬浮窗。初始位置为屏幕的左部中间靠上位置。
     *
     * @param context 必须为应用程序的Context.
     */
    public static void createVoiceWindow(Context context) {
        WindowManager windowManager = getWindowManager(context);
        screenWidth = windowManager.getDefaultDisplay().getWidth();
        screenHeight = windowManager.getDefaultDisplay().getHeight();
        if (voiceWindow == null) {
            voiceWindow = new FloatWindowVoiceView(context);
            if (voiceWindowParams == null) {
                voiceWindowParams = new WindowManager.LayoutParams();
                if (Build.VERSION.SDK_INT >= 26) { //8.0新特性
                    voiceWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                } else {
                    voiceWindowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
                }
                voiceWindowParams.format = PixelFormat.RGBA_8888;
                voiceWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                voiceWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
                voiceWindowParams.width = FloatWindowVoiceView.viewWidth;
                voiceWindowParams.height = FloatWindowVoiceView.viewHeight;
                voiceWindowParams.x = screenWidth; // 0 为左    screenWidth 为右
                voiceWindowParams.y = screenHeight / 5;
            }
        }
        voiceWindow.setParams(voiceWindowParams);
        windowManager.addView(voiceWindow, voiceWindowParams);
        showVoiceWindow = true;
    }

    /**
     * 将悬浮窗从屏幕上移除。
     *
     * @param context 必须为应用程序的Context.
     */
    public static void hideVoiceWindow(Context context) {
        if (voiceWindow != null) {
            WindowManager windowManager = getWindowManager(context);
            windowManager.removeView(voiceWindow);
//            voiceWindow = null;
        }
        showVoiceWindow = false;
    }

    /**
     * 将悬浮窗删除。
     *
     * @param context 必须为应用程序的Context.
     */
    public static void removeVoiceWindow(Context context) {
        if (voiceWindow != null && showVoiceWindow) {
            WindowManager windowManager = getWindowManager(context);
            windowManager.removeView(voiceWindow);
            voiceWindow = null;
        }
        showVoiceWindow = false;
    }

    /**
     * 是否有悬浮窗显示在屏幕上。
     *
     * @return 有悬浮窗显示在桌面上返回true，没有的话返回false。
     */
    public static boolean isWindowShowing() {
        return controlWindow != null;
    }

    /**
     * 如果WindowManager还未创建，则创建一个新的WindowManager返回。否则返回当前已创建的WindowManager。
     *
     * @param context 必须为应用程序的Context.
     * @return WindowManager的实例，用于控制在屏幕上添加或移除悬浮窗。
     */
    public static WindowManager getWindowManager(Context context) {
        if (mWindowManager == null) {
            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        return mWindowManager;
    }

    //初始化自定义View
    public static void initStatusBarHelperView(Context context) {
        WindowManager wm = getWindowManager(context);
        mStatusBarHelperView = new View(context);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        // 不可触摸，不可获得焦点
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        // 放在左上角
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        // 需要在manifest里申明android.permission.SYSTEM_ALERT_WINDOW权限
        if (Build.VERSION.SDK_INT >= 26) { //8.0新特性
            lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
        }
//        lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
        lp.format = PixelFormat.TRANSLUCENT;
        wm.addView(mStatusBarHelperView, lp);
    }

    //使用完毕移除自定义View
    public static void removeStatusBarHelperView(Context context) {
        WindowManager wm = getWindowManager(context);
        wm.removeView(mStatusBarHelperView);
        mStatusBarHelperView = null;
    }

    /**
     * @return true 状态栏隐藏;false状态栏显示
     */
    public static boolean getStatusBarHeight() {
        if (mStatusBarHelperView == null) {
            return false;
        }
        int[] windowParams = new int[2];
        int[] screenParams = new int[2];
        mStatusBarHelperView.getLocationInWindow(windowParams);
        mStatusBarHelperView.getLocationOnScreen(screenParams);
        // 如果状态栏隐藏，返回0，如果状态栏显示则返回高度
        LogUtil.d("zqh", "getStatusBarHeight = " + (screenParams[1] - windowParams[1]));
        return (screenParams[1] - windowParams[1]) == 0;
    }

    /**
     * @return true 状态栏隐藏;false状态栏显示
     */
    public static boolean getStatusBarHeight2() {
        if (mStatusBarHelperView == null) {
            return false;
        }
        int[] screenParams = new int[2];
        mStatusBarHelperView.getLocationOnScreen(screenParams);
        return screenParams[0] == 0 && screenParams[1] == 0;
    }

    public static int getStartType() {
        return START_TYPE;
    }

    public static void setStartType(int startType) {
        START_TYPE = startType;
    }
}

